package com.cloud.applet.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloud.applet.entity.*;
import com.cloud.applet.mapper.UserMapper;
import com.cloud.applet.service.UserService;
import com.cloud.applet.util.HashUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;


    @Override
    public JSONObject login(JSONObject object) {
        JSONObject jsonObject = new JSONObject();
        User user = new User();
        user.setUserName(object.get("username").toString());
        user.setPhone(object.get("phone").toString());
        String openId = HashUtil.sha256(object.get("phone").toString());
        user.setOpenId(openId);
        mapper.login(user);
        jsonObject.put("token",openId);
        return  jsonObject;
    }

    @Override
    public JSONObject addAddress(JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        Address address = new Address();
        address.setUserName(jsonObject.get("username").toString());
        address.setPhone(jsonObject.get("phone").toString());
        address.setArea(jsonObject.get("area").toString());
        address.setAddress(jsonObject.get("address").toString());
        address.setOpenId(jsonObject.get("userId").toString());
        mapper.saveAddress(address);
        result.put("code",200);
        result.put("msg","success");
        return result;
    }

    @Override
    public JSONObject getAddress(JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        List<Address> list = mapper.getAddress(jsonObject.get("userId").toString());
        result.put("data",list);
        result.put("code",200);
        result.put("msg","success");
        return result;
    }

    @Override
    public JSONObject updateAddress(JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        Address address = new Address();
        address.setId(Integer.valueOf(jsonObject.get("id").toString())) ;
        address.setUserName(jsonObject.get("username").toString());
        address.setPhone(jsonObject.get("phone").toString());
        address.setArea(jsonObject.get("area").toString());
        address.setAddress(jsonObject.get("address").toString());
        address.setOpenId(jsonObject.get("userId").toString());
        mapper.updateAddress(address);
        result.put("code",200);
        result.put("msg","success");
        return result;
    }

    @Override
    public JSONObject getGoodsList(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        String group = jsonObject.get("group").toString(); //大类
        String groupType = jsonObject.get("groupType").toString(); //小类
        List <Goods> list = mapper.getGoodsList(group,groupType);
        //前端渲染的是需要二维数组
        List<List<Goods>> formattedGoodsList = convertToNestedList(list);
        object.put("data",formattedGoodsList);
        object.put("code",200);
        return object;
    }

    @Override
    public JSONObject getGoodsById(JSONObject jsonObject) {

        JSONObject object = new JSONObject();
        Goods goods = mapper.getGoodsById(jsonObject.get("id").toString());
        object.put("data",goods);
        object.put("code",200);
        return object;
    }

    @Override
    public JSONObject getOrderList(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        List <Order> list = mapper.getOrderList(object.get("userId").toString());
        object.put("data",list);
        object.put("code",200);
        return null;
    }

    @Override
    public JSONObject deleteShopCar(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        mapper.deleteShopCar(object.get("userId").toString(),jsonObject.get("id").toString());
        return null;
    }

    @Override
    public JSONObject deleteAddress(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        mapper.deleteAddress(jsonObject.get("userId").toString(),jsonObject.get("id").toString());
        object.put("code",200);
        return null;
    }

    @Override
    public JSONObject addBag(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        ShopCar shopCar = new ShopCar();
        shopCar.setGoodsId(jsonObject.get("goodsId").toString());
        shopCar.setGoodsImg(jsonObject.get("img").toString());
        shopCar.setPrice(Integer.valueOf(jsonObject.get("money").toString()));
        shopCar.setGoodsSize("M");
        shopCar.setGoodsName(jsonObject.get("goodsName").toString());
        shopCar.setUserId(jsonObject.get("userId").toString());
        shopCar.setNum("1");
        mapper.addBag(shopCar);
        object.put("code",200);
        return object;
    }

    @Override
    public JSONObject getShopCarList(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        List<ShopCar> list =  mapper.getShopCarList(jsonObject.get("userId").toString());
        object.put("code",200);
        object.put("data",list);
        return object;
    }


    // 将商品列表转换为前端所需格式的方法
    public List<List<Goods>> convertToNestedList(List<Goods> goodsList) {
        List<List<Goods>> nestedGoodsList = new ArrayList<>();
        // 根据你的需求调整每组中的商品数量
        final int itemsPerGroup = 2;
        for (int i = 0; i < goodsList.size(); i += itemsPerGroup) {
            int end = Math.min(goodsList.size(), i + itemsPerGroup);
            nestedGoodsList.add(new ArrayList<>(goodsList.subList(i, end)));
        }
        return nestedGoodsList;
    }

}
